package com.aaa.service.impl;

import com.aaa.dao.DeptDao;
import com.aaa.entity.Dept;
import com.aaa.entity.PageSort;
import com.aaa.service.DeptService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Dept)表服务实现类
 *
 * @author
 * @since 2022-06-23 10:57:08
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Resource
    private DeptDao deptDao;

    /**
     * 通过ID查询单条数据
     *
     * @param deptno 主键
     * @return 实例对象
     */
    @Override
    public Dept queryById(Integer deptno) {
        return this.deptDao.queryById(deptno);
    }

    @Override
    public List<Dept> queryAllByLimit(int offset, int limit) {
        return null;
    }

    @Override
    public Page findAllDept(PageSort pageSort) {
        //开始分页
        PageHelper.startPage(pageSort.getPage(), pageSort.getLimit());
        List<Dept> deptList = deptDao.queryAllDept(pageSort);
        Page listWithPage = (Page) deptList;
        List result = listWithPage.getResult();
        System.out.println(result);
        long total = listWithPage.getTotal();
        System.out.println(total);
        return listWithPage;
    }


    /**
     * 新增数据
     *
     * @param dept 实例对象
     * @return 实例对象
     */
    @Override
    public Dept insert(Dept dept) {
        this.deptDao.insert(dept);
        return dept;
    }

    /**
     * 修改数据
     *
     * @param dept 实例对象
     * @return 实例对象
     */
    @Override
    public Dept update(Dept dept) {
        this.deptDao.update(dept);
        return this.queryById(dept.getDeptId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        int delete = deptDao.deleteById(id);
        return delete > 0;
    }

    @Override
    public long getRowCount(Dept dept) {
        return 0;
    }

    @Override
    public boolean deleteByIds(String[] ids) {
        int delete = deptDao.deleteByIds(ids);
        if (delete > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean findDeptName(Dept dept) {
        //true:重名
        int find = deptDao.findDeptName(dept);
        return find > 0;
    }

    @Override
    public boolean checkNotNull(String[] id) {
        //true:含有员工
        int check = deptDao.checkNotNull(id);
        return check > 0;
    }
}
